package com.hwadee.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hwadee.springboot.entity.Mistake;
import com.hwadee.springboot.entity.R;
import com.hwadee.springboot.entity.SaveWrongQuestionsRequest;
import com.hwadee.springboot.entity.WrongQuestionDto;
import com.hwadee.springboot.mapper.MistakeMapper;
import com.hwadee.springboot.service.MistakeService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

import static com.baomidou.mybatisplus.extension.toolkit.Db.save;

@Service
public class MistakeServiceImpl implements MistakeService {

    @Resource
    private MistakeMapper mistakeMapper;

    private final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    public R<Boolean> saveWrongQuestions(SaveWrongQuestionsRequest request) {
        try {
            Long studentId = request.getStudentId();
            Long examId = request.getExamId();
            List<WrongQuestionDto> wrongQuestions = request.getWrongQuestions();

            for (WrongQuestionDto wrongQuestion : wrongQuestions) {
                Mistake mistake = new Mistake();
                mistake.setStudentId(studentId);
                mistake.setExamId(examId);
                mistake.setQuestionId(wrongQuestion.getQuestionId());
                mistake.setUserAnswer(wrongQuestion.getUserAnswer());
                mistake.setCorrectAnswer(wrongQuestion.getCorrectAnswer());
                mistake.setContent(wrongQuestion.getContent());
                mistake.setOptions(wrongQuestion.getOptions());
                mistake.setBlanks(wrongQuestion.getBlanks());
                mistake.setAnswer(wrongQuestion.getAnswer());
                mistake.setExplanation(wrongQuestion.getExplanation());
                mistake.setType(wrongQuestion.getType());
                mistake.setCategory(wrongQuestion.getCategory());
                mistake.setDifficulty(wrongQuestion.getDifficulty());
                mistake.setScore(wrongQuestion.getScore());
                mistake.setIsCollected(false);

                mistakeMapper.insert(mistake);
            }

            return R.ok(true);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(500, "错题保存失败，请联系管理员");
        }

    }



    @Override
    public List<Mistake> getStudentMistakes(Long studentId) {
        LambdaQueryWrapper<Mistake> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Mistake::getStudentId, studentId);
        return mistakeMapper.selectList(queryWrapper);
    }

    @Override
    public boolean deleteMistakeByStudentAndId(Long studentId, Long mistakeId) {
        // 创建查询条件
        QueryWrapper<Mistake> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId)
                .eq("id", mistakeId);

        // 执行删除操作
        int deletedRows = mistakeMapper.delete(queryWrapper);
        return deletedRows > 0;
    }

}
